#!/usr/bin/python 
#Author:eager.yu
#Email: sanguren@qq.com

from Cloudstack_option import Cloudstack_option
import sys,re
import getopt


def usage():
    print "help---------------------------------------------------------------"
    print "need instance id to option "
    print "it is only delete owner template,the other user can't delete"
    print "command: python destroyTemplate.py -i 7b0bc00b-dca9-44f0-a833-98daec85066d"
    print "end---------------------------------------------------------------"

def main():
    id=""
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi:",["help", "id="])
    except getopt.GetoptError as err:
        print str(err) 
        usage()
        sys.exit(2)
    for o,a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif o in ("-i", "--id"):
            id=a
        else:
            assert False, "unhandled option"
    if id == "" :
        print "id can't be null"
        sys.exit()
    cs=Cloudstack_option()
    templates=cs.get_all_template()
    templates_id=[]
    for i in range(len(templates)):
        templates_id.append(templates[i]["id"])
    if not id in templates_id:
        print "the templates is not  in cloudstack"
        sys.exit()
    req=cs.delete_template(id)
    print req
    sys.exit(0)


if __name__ == "__main__":
    main()